package kzy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import kzy.common.BaseContext;
import kzy.common.R;
import kzy.entiy.Employee;
import kzy.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isNotEmpty;

/**
 * @Author 大溪地
 * @create 2023年05月24日21:32
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;


	/**
	 * 登录操作   登录逻辑
	 * @param request
	 * @param response
	 * @param employee
	 * @return
	 */
	@PostMapping("/login")
//	@RequestMapping("/login")
	public R<Employee> login(HttpServletRequest request, HttpServletResponse response, @RequestBody Employee employee){
//	1.先将页面提交的密码进行md5加密处理
		String password =employee.getPassword();
		password = DigestUtils.md5DigestAsHex(password.getBytes());

//		2.根据页面提交用户名到数据查询数据  LambdaQueryWrapper  Lambda查询包装器
		LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
//
		queryWrapper.eq(Employee::getUsername, employee.getUsername());
		Employee emp=employeeService.getOne(queryWrapper);

//3. 查询是否有该账号且密码是否正确
		if(!(emp!=null&&(password.equals(emp.getPassword())))){
		return R.error("登录失败");
		}

//4.判断账号是否被禁用
		if(emp.getStatus()==0){
			return  R.error("账号被禁用了");
		}
//		getSession 获得会话  setAttribute  设置属性  设置employee= emp.getId()  把个人信息储存到 Session里面
		request.getSession().setAttribute("employee", emp.getId());
		return  R.success(emp);
	}


	/**
	 * 退出逻辑  退出逻辑
	 * @param request
	 * @return
	 */
	@PostMapping({"/logout","/"})
	private R<String> logout(HttpServletRequest request){
//		删除 Session里面的个人信息
		request.getSession().removeAttribute("employee");
		return R.success("退出成功");
	}


	/**
	 * 添加员工
	 * @param request
	 * @param employee
	 * @return
	 */
	@PostMapping()
	private R<String> insert(HttpServletRequest request,@RequestBody Employee employee){
	//设置初始密码123456，需要进行md5加密处理
	employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
	//设置创建时间和更新时间
//	employee.setCreateTime(LocalDateTime.now());
//	employee.setUpdateTime(LocalDateTime.now());
//	log.info(LocalDateTime.now()+"当前时间为:<==");
	//获得当前登录用户的id
	Long empId = (Long) request.getSession().getAttribute("employee");
	BaseContext.setCurrentId(empId);
//	employee.setCreateUser(empId);
//	employee.setUpdateUser(empId);
	//如果这里不出异常就会return 如果出异常就会被异常处理拦截
		boolean save = employeeService.save(employee);

			return R.success("新增员工成功");
}

	/**
	 * 分页查询员工信息
	 * @param page
	 * @param pageSize
	 * @param name
	 * @return
	 */
	@GetMapping("/page")
	private R<Page> page(int page, int pageSize , String name){
		log.info(page+","+pageSize+","+name);
	//构造分页构造器
	Page pageInfo = new Page(page,pageSize);
	//构造条件构造器
	LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
	//添加过滤条件
	queryWrapper.like(isNotEmpty(name),Employee::getName,name);
	//添加排序条件
	queryWrapper.orderByDesc(Employee::getUpdateTime);

	//执行查询
	employeeService.page(pageInfo,queryWrapper);

	return R.success(pageInfo);
}

/**
 * 禁用，启用 用户操作
 */

@PutMapping()
	private R<String> disableEnable(HttpServletRequest request, @RequestBody Employee employee){
//	获取修改人的id
	Long empId =(Long) request.getSession().getAttribute("employee");
	BaseContext.setCurrentId(empId);

//	设置更新时间
//	employee.setUpdateTime(LocalDateTime.now());
//	设置更新人的id
//	employee.setUpdateUser(id);
//执行更新操作
	employeeService.updateById(employee);
//	返回信息
	return R.success("设置成功");
}


/**
 * 修改员工信息
 */
@GetMapping("/{id}")
	public  R<Employee> getById(HttpServletRequest request,@PathVariable Long id){
	log.info("getById根据id查询员工信息："+id);
	Long empId =(Long) request.getSession().getAttribute("employee");

	BaseContext.setCurrentId(empId);

//  根据id查员工信息
	Employee employee = employeeService.getById(id);
//	返回员工信息
	if(employee!=null){
		return R.success(employee);
	}
	return R.error("没有查询到该员工的信息");
}

}
